/*******************    (C) COPYRIGHT SUNSHINE SILICON    **********************
* \file
* File Name: app_key.c
* \brief
* Description:

    key funcation
    
*  Copyright (C) 2014-2035 Sunshine Silicon Corporation (www.sunsili.com), All Rights Reserved
*  SPDX-License-Identifier: Apache-2.0
*
*  Licensed under the Apache License, Version 2.0 (the "License"); you may
*  not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
*  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*
*  This file is part of bk3432 ble sdk
*
* Version && Reversed History
* Version    Date       Auther      Reversed History
  ----------------------------------------------------------------------------
  V1.0.0    2021-07-07  Lojam Fan  Fisrt Created 
  
  (C) Sunshine Silicon Corporation (www.sunsili.com)
*******************************************************************************/


 #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tuya_ble_port.h"
#include "tuya_ble_type.h"
#include "ke_mem.h"
#include "ll.h"
#include "arch.h"
#include "rf.h"
#include "flash.h"
#include "app.h"
#include "app_tuya_service.h"
#include "ke_msg.h"
#include "app_task.h"
#include "ke_timer.h"
#include "app_task.h"
#include "tuya_ble_config.h"
#include "tuya_ble_log.h"
#include "uart2.h"
#include "app_led.h"
#include "app_key.h"


key_type app_key_state;


//GPIO call back
static void app_gpio_int_cb(void);   



/*******************************************************************************
    //for 48M clock
    //100=12.5us
    //16=2us
    //8=1us
    //for 32.768 clock
    //8=1.6ms
    //16M clock
    //100=120us
*******************************************************************************/
void delay_us(uint32_t num)
{
    uint32_t i,j;
    for(i=0; i<num; i++)
    {
        for(j=0; j<3; j++)
        {
            ;
        }
    }
}

void key_wakeup_set(void)
{    
    REG_APB5_GPIO_WUATOD_ENABLE = 0x00000000; 
	REG_APB5_GPIO_WUATOD_STAT = 0xffffffff;
    REG_AHB0_ICU_INT_ENABLE &= (~(0x01 << BIT_GPIO_INT_EN)); //Disable Int
}


/*key wakeup config*/
void key_wakeup_config(void)
{
	//Clear period timer
//	ke_timer_clear(APP_PERIOD_TIMER, TASK_APP);
    gpio_wakeup_config(S1);
    REG_AHB0_ICU_INT_ENABLE |= (0x01 << BIT_GPIO_INT_EN);
}



static void app_gpio_int_cb(void)
{    
    key_wakeup_set();
//    key_status_check();
	
	if(!ke_timer_active(APP_KEY_TIMER, TASK_APP))
	{
		ke_msg_send_basic(APP_KEY_TIMER,TASK_APP,TASK_APP);
	}    
    key_wakeup_config();
    
//	TUYA_APP_LOG_INFO("%s S1:%d ", __func__, gpio_get_input(S1));
}



void app_key_init(void)
{
    gpio_config(S1, INPUT, PULL_HIGH);
    key_wakeup_config();
    //Register GPIO interrupt callback 
	gpio_cb_register(app_gpio_int_cb);
	
	//This flag is setted to allow system into sleep mode when power on.
	app_key_state = ALL_KEY_FREE;
    GLOBAL_INT_START();
//    TUYA_BLE_LOG_INFO("app_key init succese!");
}


/* key scan*/
KEY_VAL_t key_scan(void)
{
    KEY_VAL_t keyVal = NONE;
    
    if(0==gpio_get_input(S1))
    {
        delay_us(10);
        if(0==gpio_get_input(S1))
        {
           keyVal |= S1_KEY;            
        }
    }
    return keyVal;
}


/*key status check*/
void key_status_check(void)
{
    KEY_VAL_t  keyVal;
    static KEY_VAL_t lastKeyVal;
    static uint16_t s1Cnt; 
    
    keyVal = key_scan();  
    if(keyVal)
    {
        if(keyVal & S1_KEY)
        {
            if(++s1Cnt >= HOLD_5S_TIME)
            {
                s1Cnt = 0;
                app_key_state = POWER_HOLD_5S;
//                TUYA_APP_LOG_INFO("%s keyVal: %d, lastkeyVal: %d%, keyState: %d, cnt:%d\r\n", __func__, keyVal, lastKeyVal, app_key_state, s1Cnt);
                return;
            }            
        }
        ke_timer_set(APP_KEY_TIMER, TASK_APP, KEY_SCAN_DUTY); 
    }    
    else if(lastKeyVal != keyVal)
    {
        if(lastKeyVal & S1_KEY)
        {   
            if(s1Cnt < HOLD_5S_TIME)
            { 
                s1Cnt = 0;                
                app_key_state = POWER_KEY_DOWN;
            }            
            
        }           
        ke_timer_set(APP_KEY_TIMER, TASK_APP, KEY_SCAN_DUTY); 
        ke_msg_send_basic(APP_LED_TIMER,TASK_APP,TASK_APP);
    }
    else
    {
         app_key_state = ALL_KEY_FREE;
    }
     
//    TUYA_APP_LOG_INFO("%s keyVal: %d, lastkeyVal: %d%, keyState: %d, cnt:%d\r\n", __func__, keyVal, lastKeyVal, app_key_state, s1Cnt);
    lastKeyVal = keyVal;
}



void app_key_timer_callback(void)
{
//    key_scan();    
   key_status_check();
}

/******************* (C) COPYRIGHT SUNSHINE SILICON  **************************
********************        END OF FILE app_key.c        *******************/

